home *** CD-ROM | disk | FTP | other *** search
- Path: news.mistral.co.uk!usenet
- From: mikebarnard@mistral.co.uk (Mike Barnard)
- Newsgroups: comp.os.msdos.programmer,comp.lang.c
- Subject: SUUMMARY - my structure and pointer problems cured.
- Date: Sat, 09 Mar 1996 02:06:51 GMT
- Organization: Mistral Internet (Brighton)
- Message-ID: <4hr0v5$4gf@news.mistral.co.uk>
- NNTP-Posting-Host: l61.mistral.co.uk
- X-Newsreader: Forte Free Agent 1.0.82
-
- Hi.
-
- I recently posted about a function that would not recognise
- a structure and it's variables. Thanks to all who replied,
- and I have the problem fixed now. I hope this information
- may be of some use. Especially to newbies like me who don't
- know about these sorts of facts.
-
- The intention...
-
- Using Borland TC3, I had 3 functions. "Main", "Vpcmenu" and
- "Prntitem". "Main" printed a title box then called "vpcmenu"
- to display and get input about a main menu. This declared a
- structure to hold information about the individual menu
- items. I delared an array of 9 structures and a pointer to
- them, then initialised them with thier data. In a loop I
- passed the value of the pointer to the function "prntitem",
- whose job was to print the contents of the pointed-to
- structure on the screen in the correct place.
-
- The problem...
-
- I had a compiler error in the function "prntitem" where I
- tried to use the variables within the structure. The error
- said the variables were undeclared. I was also told that the
- pointer I passed was never used. I tried moving the
- structure declaration to global and just inside main but no
- dice; the function "prntitem" didn't recognise the
- structure.
-
- The answer...
-
- It was because I was using seperate source files for each of
- the functions. I assumed that because the structure had been
- defined 'upwind' of the function that wanted to use it the
- compiler would just take the data from each file, make a
- program and the scope would be OK . Oh, no! In Borland at
- least (I don't know about other compilers) structure
- definitions, includes and god only knows what else is only
- valid for the source file it is in. Split into multiple
- source files and you have to repeat the declarations.
-
- The cure...
-
- Creating a header file with the information in it and
- including it in each and every source file. The following is
- a better explanation cut from a reply... my thanks to
- "tljones1" and all the others who replied.
-
- -----------------------------------------------------------
- > files shouldn't matter? The compiler should sort that out shouldn't
- > it? Maybe not...
-
- nope... the compiler won't sort things out for you. What is
- defined in one source file isn't in another. Believe it or
- not, but it keeps you from running into problems. You have
- to explicitly declare your types in each source file you
- intend to use them in. and the best way to do that is to
- have a include file that declares that.. For example
- mystuff.h would have all of the declarations for the source
- file mystuff.cpp (so it has all of the structure
- declarations, global variables, function prototypes, etc.)
- and mystuff.cpp would include mystuff.h (it is also standard
- that mystuff.h has all of the other inncludes that
- mystuff.cpp would need). Also another other source file
- that needed to use the types declared in mystuff.h would
- also include mystuff.h
-
- I hope this makes sense... take it easy...
- -----------------------------------------------------------
-
- If anything I've said is inaccurate, please publicly correct
- me. But this has worked for me. Now for the next bit,
- getting input and checking for specific keypresses. (Arrows,
- numbers and enter)... sigh. I'll get there in the end. With
- the help of some friends. Thanks.
-
-
- ---
- Mic.
- From windy, damp, but clearing skies Worthing; England.
- mikebarnard@mistral.co.uk
-
-